export const ADD_COMMENTS = 'ADD_COMMENTS'
export const DELETE_COMMENTS = 'DELETE_COMMENTS'
export const INIT_COMMENTS = 'INIT_COMMENTS'

const initState = []
const reducer = (state = initState, action) => {
  switch (action.type) {
    case INIT_COMMENTS:
      return action.comments
    case ADD_COMMENTS:
      return [
        ...state,
        action.comment,
      ]
    case DELETE_COMMENTS:
      let comments = state.slice(0);
      comments.splice(action.index, 1)
      return comments
    //遇到未知的 action 时，一定要返回旧的 state
    default:
      return state;
  }
}

export default reducer

/**
 * actions
 */
// 只是一个方便，避免每次都手动去写 dispatch({type: 'xxx', xxx});
export const addComment = (comment) => {
  return { type: ADD_COMMENTS, comment}
}
export const deleteComment = (index) => {
  return { type: DELETE_COMMENTS, index}
}
export const initComments = (comments) => {
  return { type: INIT_COMMENTS, comments}
}

// 更偷懒，直接用redux-actions提供的createAction,handleAction, 建议初学者别用...
// import {createAction} from 'redux-actions'
// export const initComments = createAction(INIT_COMMENTS)
